package com.llsydn.demo.sms;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.llsydn.hello.R;

/**
 * @author lilinshen
 * @title 发送短信的activity
 * @description 请填写相关描述
 * @date 2018/9/19 11:18
 */
public class SmsActivity extends AppCompatActivity implements View.OnLongClickListener {
    private EditText et_main_number;
    private EditText et_main_sms;
    private Button btn_main_call;
    private Button btn_main_send;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        //初始化视图对象
        et_main_number = findViewById(R.id.et_main_number);
        et_main_sms = findViewById(R.id.et_main_sms);
        btn_main_call = findViewById(R.id.btn_main_call);
        btn_main_send = findViewById(R.id.btn_main_send);

        //给视图对象设置点击监听
        btn_main_call.setOnClickListener(onClickListener);
        btn_main_send.setOnClickListener(onClickListener);
        //给视图对象设置长按监听
        btn_main_call.setOnLongClickListener(this);
        btn_main_send.setOnLongClickListener(this);
    }

    /**
     * 点击事件
     */
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_main_call) {
                //Toast.makeText(SmsActivity.this, "点击打电话", Toast.LENGTH_SHORT).show();
                //1).创建一个Intent(隐式意图)
                String action = Intent.ACTION_DIAL;
                Intent intent = new Intent(action);

                //2).携带数据
                String number = et_main_number.getText().toString();
                intent.setData(Uri.parse("tel:" + number));

                //3).startActivity
                startActivity(intent);
            } else if (v == btn_main_send) {
                //Toast.makeText(SmsActivity.this, "点击发短信", Toast.LENGTH_SHORT).show();

                //1).创建一个Intent(隐式)
                String action = Intent.ACTION_SENDTO;
                Intent intent = new Intent(action);

                //2).携带数据(号码，内容)
                String number = et_main_number.getText().toString();
                String sms = et_main_sms.getText().toString();
                intent.setData(Uri.parse("smsto:" + number));
                intent.putExtra("sms_body", sms);

                //3).startActivity
                startActivity(intent);
            }
        }
    };

    /**
     * 长按事件
     *
     * @param v
     * @return
     */
    @Override
    public boolean onLongClick(View v) {
        // 长按打电话，需要打电话的权限(android.permission.CALL_PHONE)
        if (v == btn_main_call) {
            //Toast.makeText(SmsActivity.this, "长按打电话", Toast.LENGTH_SHORT).show();
            //1).创建一个Intent(隐式意图)
            String action = Intent.ACTION_CALL;
            Intent intent = new Intent(action);
            //2).携带数据
            String number = et_main_number.getText().toString();
            intent.setData(Uri.parse("tel:" + number));
            //3).startActivity
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0);
            } else {
                startActivity(intent);
            }
        } else if (v == btn_main_send) {
            //Toast.makeText(SmsActivity.this, "长按发短信", Toast.LENGTH_SHORT).show();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE}, 0);
            } else {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
                } else {
                    //1).得到SmsManager对象
                    SmsManager smsManager = SmsManager.getDefault();
                    //2).发送文本信息(短信)
                    String number = et_main_number.getText().toString();
                    String sms = et_main_sms.getText().toString();
                    smsManager.sendTextMessage(number, null, sms, null, null);
                }
            }
        }
        return true;//表示此事件已经被消费了,不会再触发点击事件
    }
}
